package com.ruoyi.durian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ALiYunAISDKUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.durian.domain.LlCoupon;
import com.ruoyi.durian.domain.LlThali;
import com.ruoyi.durian.domain.dto.CouponInfoDto;
import com.ruoyi.durian.domain.dto.ThaliInfoDto;
import com.ruoyi.durian.domain.vo.ThaliVo;
import com.ruoyi.durian.mapper.LlCouponMapper;
import com.ruoyi.durian.service.LlThaliService;
import com.ruoyi.durian.mapper.LlThaliMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lenovo
 * @description 针对表【ll_thali(优惠券模块-套餐表)】的数据库操作Service实现
 * @createDate 2024-07-26 16:17:15
 */
@Service
public class LlThaliServiceImpl extends ServiceImpl<LlThaliMapper, LlThali>
        implements LlThaliService {

    @Resource
    private LlThaliMapper llThaliMapper;

    @Resource
    private LlCouponMapper llCouponMapper;



    /**
     * 查询优惠券模块-套餐
     *
     * @param id 优惠券模块-套餐主键
     * @return 优惠券模块-套餐
     */
    @Override
    public LlThali selectLlThaliById(Long id) {
        return llThaliMapper.selectLlThaliById(id);
    }

    /**
     * 查询优惠券模块-套餐列表
     *
     * @param llThali 优惠券模块-套餐
     * @return 优惠券模块-套餐
     */
    @Override
    public List<LlThali> selectLlThaliList(LlThali llThali) {
        return llThaliMapper.selectLlThaliList(llThali);
    }

    /**
     * 发布套餐
     *
     * @param llThali 优惠券模块-套餐
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertLlThali(LlThali llThali) {
        if (llThali == null && llThali.getCouponId() != null
                && llThali.getCouponNum() != null && llThali.getCouponNum() > 0) {
            return AjaxResult.error("参数错误");
        }
        LlCoupon llCoupon = llCouponMapper.selectById(llThali.getCouponId());
        if (llCoupon == null) {
            return AjaxResult.error("优惠券不存在");
        }

        BigDecimal saleMoney = llCoupon.getSaleMoney();
        Integer couponNum = llThali.getCouponNum();
        llThali.setMaxMoney(saleMoney.multiply(BigDecimal.valueOf(couponNum))
                .setScale(2, BigDecimal.ROUND_HALF_UP));
        llThali.setCreateTime(new Date());
        llThali.setUpdateTime(new Date());
        llThali.setStatus(0);
        llThali.setIsDelete(0);
        llThali.setCreateTime(DateUtils.getNowDate());
        llThali.setIsDelete(0);
        llThaliMapper.insertLlThali(llThali);
        return AjaxResult.success();
    }

    /**
     * 修改优惠券模块-套餐
     *
     * @param llThali 优惠券模块-套餐
     * @return 结果
     */
    @Override
    public AjaxResult updateLlThali(LlThali llThali) {
        llThali.setUpdateTime(DateUtils.getNowDate());
        return AjaxResult.success(llThaliMapper.updateLlThali(llThali));
    }

    /**
     * 批量删除优惠券模块-套餐
     *
     * @param ids 需要删除的优惠券模块-套餐主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteLlThaliByIds(Long[] ids) {
        return AjaxResult.success(llThaliMapper.deleteLlThaliByIds(ids));
    }


    /**
     * 套餐列表
     *
     * @return
     */
    @Override
    public List<ThaliInfoDto> thaliList() {
        List<ThaliInfoDto> llThalis = llThaliMapper.selelctAll();
        return llThalis;
    }
    /**
     * 套餐详情
     *
     * @return
     */
    @Override
    public AjaxResult thaliInfo(Integer id) {
        if (id == null) {
            return AjaxResult.error("参数错误");
        }
        return AjaxResult.success(llThaliMapper.selectByThaliIdOnInFo(id));
    }

    /**
     * 删除套餐
     *
     * @return
     */
    @Override
    public AjaxResult delThali(Integer id) {
        if (id == null) {
            return AjaxResult.error("参数错误");
        }
        LlThali llThali = llThaliMapper.selectById(id);
        if (llThali != null && llThali.getType() == 1) {
            return AjaxResult.error("套餐下架才能删除");
        }
        llThaliMapper.deleteById(id);
        return AjaxResult.success();
    }

    /**
     * 我的卡券列表
     * @return
     */
    @Override
    public List<CouponInfoDto> myCardList() {
        Long userId = SecurityUtils.getUserId();
        List<CouponInfoDto> llThalis = llThaliMapper.selectMyCard(userId);
        return llThalis;
    }

}




